package com.zhao.dao;

import com.zhao.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

@Mapper
@Component
public interface UserDao{

	//插入
	@InsertProvider(type = SqlProvider.class,method = "insert")
	Integer insert(User record);

	//更新
	@UpdateProvider(type = SqlProvider.class,method = "update")
	Integer update(User record);

	//删除
	@DeleteProvider(type = SqlProvider.class,method = "delete")
	Integer delete(User record);

	class SqlProvider{

		public static String insert(final User record){
			return new SQL(){{
				INSERT_INTO("USER");
				VALUES("ID", "#{id}");
				VALUES("USER_ID", "#{userId}");
				VALUES("USER_NAME", "#{userName}");
				VALUES("AGE", "#{age}");
			}}.toString();
		}

		public static String update(final User record){
			return new SQL(){{
				UPDATE("USER");
				if(record.getUserName()!=null){
					SET("USER_NAME=#{userName}");
				}
				if(record.getAge()!=null){
					SET("AGE=#{age}");
				}
				WHERE("ID=#{id}","USER_ID=#{userId}");
			}}.toString();
		}

		public static String delete(final User record){
			return new SQL(){{
				DELETE_FROM("USER");
				WHERE("ID=#{id}","USER_ID=#{userId}");
			}}.toString();
		}
	}

}